package servlet;

import dao.StudentD;
import dao.TeacherD;
import vo.Student;
import vo.Teacher;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

//处理注册信息, 若成功则跳转登录界面, 否则给出错误提示
@WebServlet("/check_register")
public class check_register extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        String email = request.getParameter("email");
        String user = request.getParameter("user");
        String role = request.getParameter("role");
        String password = request.getParameter("password1");
        String code = request.getParameter("code");

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        String randStr = (String) session.getAttribute("randStr");

        if (!code.equals(randStr)) {
            out.print("<script>alert('验证码错误！');location.href = 'register.jsp';</script>");
        } else {
            if(role.equals("teacher")){
                TeacherD teacherD = new TeacherD();
                Teacher teacher = null;

                try {
                    if(!user.matches("\\d{6}")) {
                        out.print("<script>alert('教师编号格式不对，请输入六位有效编号！');location.href = 'register.jsp';</script>");
                    }else {
                        teacher = teacherD.insertTeacher(user, password, email);
                    }
                } catch (Exception e) {
                    out.print(e);
                }
                if (teacher != null) {
                    //向session中添加用户信息
                    session.setAttribute("info", teacher);
                    response.sendRedirect("one_page_student");
                } else {
                    out.print("<script>alert('该用户已被注册！');location.href = 'register.jsp';</script>");
                }
            }else {
                StudentD studentD = new StudentD();
                Student student = null;

                try {
                    if(!user.matches("(19|20)\\d{2}[0-9]{6}")) {
                        out.print("<script>alert('学号格式不对，请输入十位有效学号！');location.href = 'register.jsp';</script>");
                    }else {
                        student = studentD.insertStudentRegister(user, password, email);
//                        System.out.println(student);
                    }
                } catch (Exception e) {
                    out.print(e);
                }
                if (student != null) {
                    //向session中添加用户信息
                    session.setAttribute("info", student);
                    response.sendRedirect("student/main.jsp");
                } else {
                    out.print("<script>alert('该用户已被注册！');location.href = 'register.jsp';</script>");
                }
            }
        }
    }
}
